package com.zhangxun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.zhangxun.domain.Basket;
import com.zhangxun.service.BasketService;
import com.zhangxun.util.SecurityUtil;
import com.zhangxun.vo.CartMoney;
import com.zhangxun.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车接口管理")
@RestController
@RequestMapping("/p/shopCart")
public class CartController {

    @Autowired
    private BasketService basketService;

    @GetMapping("prodCount")
    @ApiOperation(value = "查询购物车商品数量接口")
    public ResponseEntity<Integer> prodCount(){

        int count = basketService.count(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getUserId, SecurityUtil.getWxUserId()));

        return ResponseEntity.ok(count);

    }

    @GetMapping("/info")
    @ApiOperation(value = "购物车详情接口")
    public ResponseEntity<CartVo> info(){
        CartVo cartVo = basketService.selectInfo();
        return ResponseEntity.ok(cartVo);

    }

    @PostMapping("changeItem")
    @ApiOperation(value = "添加商品到购物车和修改购物车商品数量接口")
    public ResponseEntity<Boolean> changeItem(@RequestBody Basket basket){

       Boolean result = basketService.saveOrUpdateBasket(basket);

       return ResponseEntity.ok(result);

    }

    @PostMapping("totalPay")
    @ApiOperation(value = "计算购物车选中商品总价接口")
    public ResponseEntity<CartMoney> totalPay(@RequestBody List<Long> basketIds){

        CartMoney cartMoney = basketService.totalPay(basketIds);
        return ResponseEntity.ok(cartMoney);
    }


    @DeleteMapping("/deleteItem")
    @ApiOperation("删除购物车商品接口")
    public ResponseEntity<Boolean> deleteItem(@RequestBody List<Long> basketIds){
        boolean b = basketService.removeByIds(basketIds);

        return ResponseEntity.ok(b);
    }



    @PostMapping("/getByCondition")
    public List<Basket> getByCondition(@RequestBody List<Long> basketIdList){

        List<Basket> basketList = basketService.listByIds(basketIdList);
        return basketList;
    }


    /**
     * y远程掉哟给通过id删除购物车商品
     * @param basketIds
     */
    @PostMapping("/deleteByIds")
    public void deleteByIds(@RequestBody List<Long> basketIds){

        basketService.removeByIds(basketIds);

    }

}